package com.lujianfei.mqtt.common

import com.blankj.utilcode.util.PathUtils
import com.tencent.mmkv.MMKV

/**
 * @Author: wn
 * @Date: 2024/4/18 17:15
 * @Description: 腾讯 MMKV key value 存储
 */
object KVSpUtils {
  private val kv: MMKV by lazy {
    MMKV.defaultMMKV()
  }

  init {
    MMKV.initialize(PathUtils.getInternalAppFilesPath())
  }

  fun put(key: String, value: Any?) {
    when (value) {
      is Boolean -> kv.putBoolean(key, value)
      is ByteArray -> kv.putBytes(key, value)
      is Float -> kv.putFloat(key, value)
      is Int -> kv.putInt(key, value)
      is Long -> kv.putLong(key, value)
      is String -> kv.putString(key, value)
      else -> error("${value?.javaClass?.simpleName} Not Supported By CnSpUtils")
    }
  }

  fun getBoolean(key: String, defValue: Boolean = false) = kv.getBoolean(key, defValue)

  fun getBytes(key: String, defValue: ByteArray? = null) = kv.getBytes(key, defValue)

  fun getFloat(key: String, defValue: Float = 0f) = kv.getFloat(key, defValue)

  fun getInt(key: String, defValue: Int = 0) = kv.getInt(key, defValue)

  fun getLong(key: String, defValue: Long = 0L) = kv.getLong(key, defValue)

  fun getString(key: String, defValue: String? = null) = kv.getString(key, defValue)

  //  根据 key 清除 value  置空当前字段的value  俩一样
  fun remove(key: String) = kv.remove(key)

  fun removeValue(key: String) = kv.removeValueForKey(key)
}